package cn.wolfcode.shop.cloud.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

public class CookieUtil {

    public static void addCookie(String cookieName, String cookieValue, int expireTime, HttpServletResponse response){
        Cookie cookie = new Cookie(cookieName, cookieValue);
        cookie.setDomain("localhost");
        cookie.setMaxAge(expireTime);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    public static String getToken(String userToken, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie:cookies){
            if(cookie.getName().equals(userToken)){
                return cookie.getValue();
            }
        }
        return null;
    }
}
